//Called when the header checkbox is clicked
function onHeaderCheckboxClicked(headerId, additionalFunc){
    var myHeaderCheckbox = document.getElementById(headerId);
    handleHeaderStateChange(myHeaderCheckbox, checkedHiddenIds);
    var headerChecked = myHeaderCheckbox.checked;
    if(headerChecked){
    	myHeaderCheckbox.setAttribute("title", "Click to de-select all");
    }
    else{ 
    	myHeaderCheckbox.setAttribute("title", "Click to select all");
    }
    
    // If additional function is defined
    if (additionalFunc )
    	window[headerCheckboxAdditionalFunc]();
}

//Helps in drawing the checkbox for all rows !
function formatCheckboxCell(elCell, oRecord, oColumn, oData){
    //Create checkbox
    var checkboxId = 'cb_' + oRecord.getData()[fieldToSave];
    var checkbox = document.getElementById(checkboxId);
    if (checkbox == null){
        checkbox = document.createElement('input');
        checkbox.setAttribute('type', 'checkbox');
        checkbox.setAttribute('id', checkboxId);
        checkbox.setAttribute('value', oRecord.getData()[fieldToSave]);
        //if (checkboxStyle != null) {
        //	 checkbox.setAttribute('style',checkboxStyle);
        //}
        //checkbox.setAttribute('style','padding-left:5px');
        checkbox.setAttribute('class', YAHOO.widget.DataTable.CLASS_CHECKBOX);
        elCell.innerHTML = "";
        elCell.appendChild(checkbox);
    }
}

//Handles the header checkbox for select/deselect all
//Calls the fireEvent(checkboxClickEvent) for the datatable "myDataTable"
//This will call the subscribe functions declared in the datatabletag and
//will handle populating/depopulating the hidden fields
//Integrated with paginator -- if there is no paginator this will need to be refactored
function handleHeaderStateChange(myHeaderCheckbox, hiddenFieldId){
    var headerChecked = myHeaderCheckbox.checked;
    var myDataRecords = myDataTable.getRecordSet();
    var recordCount = myDataRecords.getLength();
    var paginator = myDataTable.get('paginator');
    var cur = paginator.getState();
    var begin = cur.recordOffset;
    var perPage = cur.rowsPerPage;

    if (recordCount > 0) {
    	
    	// Check to make sure we don't walk off the end of the page
    	// Bugfix:8714
    	var lastRecord = (begin + perPage > recordCount) ? recordCount : begin + perPage;
    	for (var i = begin; i < lastRecord; i++){
	        var cb = null;
	        if(headerChecked){
	        	
	        	//gets the record that was setup in the checkboxFormatter for the datatable
	        	cb = document.getElementById("cb_" + myDataRecords.getRecord(i).getData(fieldToSave));
	        	//only fire event if not checked
	        	if (!cb.checked){
	        		cb.checked = true;
	        		myDataTable.fireEvent("checkboxClickEvent",{target:cb});
	        	}
	        }
	        else{
	        	cb = document.getElementById("cb_" + myDataRecords.getRecord(i).getData(fieldToSave));
	
	        	if (cb.checked){
	        		cb.checked = false;
	        		myDataTable.fireEvent("checkboxClickEvent",{target:cb});
	        	}
	        }
	    }
    }
    else{
    	myHeaderCheckbox.checked = false;
    }
}

function addToHiddenList(hiddenFieldId, valueToAdd, additionalFunc){
   var hiddenIdStorage = document.getElementById(hiddenFieldId).value;
   var hiddenArray = hiddenIdStorage.split(",");
   if (hiddenIdStorage == ""){hiddenArray.pop();};  //Remove the empty record
   hiddenArray.push(valueToAdd);
   document.getElementById(hiddenFieldId).value = hiddenArray.toString();
   updateCounts(hiddenArray.length);
   
   // If additional function is defined
   if (additionalFunc )
	   window[headerCheckboxAdditionalFunc]();
}

function removeFromHiddenList(hiddenFieldId, valueToRemove, additionalFunc){
   var hiddenIdStorage = document.getElementById(hiddenFieldId).value;
   var hiddenArray = hiddenIdStorage.split(",");
   var removeIdx = hiddenArray.indexOf(valueToRemove);
   //removes the element, modifies the array
   hiddenArray.splice(removeIdx, 1);
   document.getElementById(hiddenFieldId).value = hiddenArray.toString();
   updateCounts(hiddenArray.length);
   if (additionalFunc )
	   window[headerCheckboxAdditionalFunc]();
}

//var REGEX_PREFIX = "([^\\^\\|]+\\|){4}";
//var REGEX_SUFFIX = "\\^";
function getRegex(reportId){
	var regex = REGEX_PREFIX + reportId + REGEX_SUFFIX;
	return regex;
}

function addToHiddenDelimList(hiddenFieldId, value){
   var hiddenIdStorage = document.getElementById(hiddenFieldId).value;
   hiddenIdStorage += value; 
   document.getElementById(hiddenFieldId).value = hiddenIdStorage;
}

function removeFromHiddenDelimList(hiddenFieldId, value){
   var hiddenIdStorage = document.getElementById(hiddenFieldId).value;
   var regex = new RegExp(getRegex(value));
   var newDelimList = hiddenIdStorage.replace(regex, "");
   document.getElementById(hiddenFieldId).value = newDelimList;
}

// Add some convenience methods onto the prototype
YAHOO.widget.DataTable.prototype.check = function (e) {
   var cb = e.el.cells[0].getElementsByTagName('input')[0];
   cb.checked = true;
};
YAHOO.widget.DataTable.prototype.uncheck = function (e) {
   var cb = e.el.cells[0].getElementsByTagName('input')[0];
   cb.checked = false;
};